
const express = require("express");
const router = express.Router();
const pool = require('../pool');
const {randomCode,sendCode}=require("../utils/phoneMessage");

let userNameArr = ['WearMask','风花雪月','十月围城','革命仍未成功','十月革命','晋南风采']


router.get("/verification",(req,res)=>{
    let code= '0000';//生成6位数字随机验证码
    let phone = req.query.phone
    var date = new Date();
    var isLive = "no";
    //'phone'需输入用户手机号
    pool.query('select u_id from user where phone=?',[phone],async(err,r)=>{
        if(err){
            return next(err)
        }
        if(r.length>0){
            pool.query('select u_id from user where nickname is null',(err,resq)=>{
                if(err){
                    return next(err)
                }
                if(resq.length > 0){
                    let id = resq[0]['u_id']
                    pool.query(`update user set code=? where u_id=${id}`,[code],(err,r)=>{
                        if(err){
                            return next(err)
                        }
                        sendCode(phone,code,function(success){
                            if(success){
                                res.send("短信验证码已发送");
                            }else{
                                res.send("短信验证码发送失败");
                            }
                        })
                    })
                }else{
                    res.send({success:false,message:"电话已注册"})
                }
            })
           
            return
        }else{
             pool.query("insert into user values(null,?,?,default,default,?,default,default,?,?,?)",[null,null,date,phone,code,isLive],(err,r)=>{
            if(err){
                return next(err)
            }
            sendCode(phone,code,function(success){
                    if(success){
                        res.send("短信验证码已发送");
                    }else{
                        res.send("短信验证码发送失败");
                    }
                })
           });
           
        }
    })
   
})


router.post('/doRegister',async (req,res,next)=>{ 
    var username = userNameArr [Math.floor(Math.random()*(userNameArr.length-1))] ;
    var password = req.body.password;
    var phone  = req.body.phone;
    var code = req.body.code;
    var nowDate = (new Date()).getTime();
    pool.query('select * from user  where nickname =?',[username],(err,resq)=>{
        if(resq.length>0){
            res.send({success:false,message:"用户已注册"})
        }else{
            pool.query('select * from user where phone=?',[phone],(err,r)=>{
                if(err){
                    return next(err)
                }
                console.log(r)
                if(r[0].code=== code && (r[0].birthday.getTime()) - nowDate < 600000){
                    pool.query('update user set nickname=?,pwd=?,birthday=?,phone=?,isLive=? where phone = ?',[username,password,r[0].birthday,phone,"yes",phone])
                    res.send({success:true,message:'注册成功'})
                }else{
                    res.send({success:false,message:'验证码过期'})
                }
            })
        }
    })
  
    
 
});

module.exports = router;
